#include "inet.h"

int  open_socket(long host , int port)
{
   int sockfd = -1;
   struct sockaddr_in serv_addr;
  
   if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
       err_dump("server: cant open stream socket");

   //setnonblocking(sockfd);
   //epoll_add(sockfd,EPOLLIN,EPOLLET);

   bzero((char*)&serv_addr,sizeof(serv_addr));
   serv_addr.sin_family      = AF_INET;
   serv_addr.sin_addr.s_addr = htonl(host);
   serv_addr.sin_port         = htons(port);

   if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1)
      err_dump("server: cant bind local address");

   if(listen(sockfd,1024) == -1)
      err_dump("server: cant listen");

   return sockfd;
}

void setnonblocking(int fd){
   int opts;
   opts = fcntl(fd,F_GETFL);
   if(opts < 0)
      err_sys("fcntl F_GETFL %d:error",fd);

   opts |= O_NONBLOCK; 
  
   if(fcntl(fd,F_SETFL,opts)<0)
      err_sys("fcntl F_SETFL %d:error",fd);
}
